package cn.bonoon.kernel.web.handlers.loader;

import java.lang.reflect.AnnotatedElement;

import cn.bonoon.handler.AutoManager;
import cn.bonoon.kernel.events.ReadEvent;
import cn.bonoon.kernel.support.searcher.Finder;
import cn.bonoon.kernel.support.services.ISearchable;
import cn.bonoon.kernel.web.annotations.AutoDataLoader;
import cn.bonoon.kernel.web.models.IdParameter;

/**
 * 用于treegrid的ajax模式加载数据
 * @author jackson
 *
 * @param <E>
 * @param <N>
 */
public class AjaxNodeLoaderHandler extends AbstractSearchableLoaderHandler{
	
	@Override
	public Object buildParams() {
		return new IdParameter();
	}
	
	public AjaxNodeLoaderHandler(ISearchable searchable, Class<?> resultClass, AnnotatedElement aEle) throws Exception {
		super(searchable, resultClass, null, aEle);
	}

	public AjaxNodeLoaderHandler(AutoManager manager, Class<?> resultClass, AutoDataLoader loader) throws Exception {
		super(manager, resultClass, null, loader);
	}
	
	@Override
	protected Object __parseResult(Finder finder, ReadEvent event) throws Exception {
		Object condition = event.getSource();
		Long id = null;
		if(condition instanceof IdParameter){
			id = ((IdParameter) condition).getId();
		}
		return finder.tryLoadChildren(id);
	}

//	@Override
//	public List<?> execute(ReadEvent event) throws Exception {
//		Object condition = event.getSource();
//		Long id = null;
//		if(condition instanceof IdParameter){
//			id = ((IdParameter) condition).getId();
//		}
//		return searcher.tryLoadChildren(id, event, resultClass);
//	}
}
